﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;

/// <summary>
/// Summary description for child
/// </summary>
/// 
namespace Bestlife.ERP.DataAccess
{


    public class D_child :System.Web.UI.Page
    {

        
        SqlDataReader rdR_Status;
        List<string> childlist = new List<string>();
        String parentStatus;
        String returnID;

        public D_child()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region update child details

        public Boolean  Updatechild(String id, Double TotalCommitions,double bv,double pv,double directcomm)
        {
            Bestlife.ERP.DataAccess.D_bonus dbns=new D_bonus();
            Bestlife.ERP.Entities.E_Bonus bnstable = new Entities.E_Bonus();
            bnstable = dbns.Getbonus(id);

            Boolean upd2 = false;
            try
            {

                 BusinessRules.Check_montlyPV mp = new BusinessRules.Check_montlyPV();
                 Boolean chkp = mp.check_monthlyPV(id, User_Ststus(id));
                 BusinessRules.update_Status ups = new BusinessRules.update_Status();
                 double storecomm= ups.storeComm(id, directcomm);


                      if (chkp == true)
                      {

                          String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                          Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                          String updtxt2 = "UPDATE Bonus SET  T_commision ='" + TotalCommitions + "',Last_Modify='" + System.DateTime.Now + "' WHERE User_id='" + id + "'";
                          // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
                          upd2 = clsCon.AddEditDel(updtxt2);
                      }else
                      {
                          Bestlife.ERP.DataAccess.D_bonus bns = new DataAccess.D_bonus();//(Add by chathuranga)
                          Bestlife.ERP.Entities.E_Bonus ebns1 = new Entities.E_Bonus();//(Add by chathuranga)
                          ebns1 = bns.Getbonus(id);

                          double allbv = double.Parse(ebns1.Storebv.ToString())+ bv;
                          double allpv = double.Parse(ebns1.Storepv.ToString()) + pv;

                          String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                          Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                          String updtxt2 = "UPDATE Bonus SET   Store_comm='" + storecomm + "', Store_bv='" + allbv + "', Store_pv='" + allpv + "',Last_Modify='" + System.DateTime.Now + "' WHERE User_id='" + id + "'";
                          // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
                          upd2 = clsCon.AddEditDel(updtxt2);
                      }
            }

            catch (Exception err)
            {
                
                Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }

            finally
            {
                clsCon.conn_new.Close();
               
            }

            return upd2;
        }

        #endregion

        # region Get User_Ststus- parent ID
        public String User_Ststus(String id)
        {

            try
            { //try
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "SELECT user_Status from Bonus  where User_id='" + id + "' ";
                //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);


                rdR_Status = clsCon.ReadTable(Ssql);


                if (rdR_Status.Read())
                {
                    parentStatus = rdR_Status["user_Status"].ToString();
                   
                }

            } //try

            catch (Exception err)
            {

                Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
                
            }


            finally
            {
                clsCon.conn_new.Close();
                rdR_Status.Close();

            }   //finally

            return parentStatus;

        }//close method

        #endregion

        #region getchilds by ID

        public List<String> childs(String id)
              {

                  try
                  { //try
                      String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                      Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                      String Ssql = "SELECT d.Dis_ID from Tbl_Distributor d,Distributor_Level b where d.Sup_ID='"+id+"' AND d.Sup_ID=b.User_id";
                      //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);                    
                      rdR_Status = clsCon.ReadTable(Ssql);


                     while(rdR_Status.Read())
                      {
                          returnID = rdR_Status["Dis_ID"].ToString();
                          childlist.Add(returnID);
                      }

                  } //try

                  catch (Exception err)
                  {

                      //Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
                  }

                    
                  finally
                   
                  {
                     clsCon.conn_new.Close();
                     rdR_Status.Close();

                  }   //finally

                  return childlist;

              }//close method

        #endregion

    }
}